//
//  SLNetNotifacationManageer.m
//  ShunLianPower2
//
//  Created by 陈镇 on 2017/12/2.
//  Copyright © 2017年 陈镇. All rights reserved.
//

#import "SLNetworkMonitorManager.h"
#import <RealReachability/RealReachability.h>
#import <netinet/in.h>

//#import "BaiduApiManager.h"
//#import "ChatHttpRequest.h"
@interface SLNetworkMonitorManager()

@property (nonatomic, weak) AFNetworkReachabilityManager *networkReachabilityManager;

@end
@implementation SLNetworkMonitorManager

+ (instancetype)sheardManager {
    static SLNetworkMonitorManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[SLNetworkMonitorManager alloc] init];
    });
    return manager;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.isHaveNet = YES;
        self.networkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
        __weak typeof(self) weakSelf = self;
        [self.networkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            weakSelf.networkReachabilityStatus = status;
            [weakSelf networkStatusChange];
            if (status == AFNetworkReachabilityStatusNotReachable)
            {
                weakSelf.isHaveNet = NO;
            }else{
                weakSelf.isHaveNet = YES;
               //有网监听回调
//                if(![BaiduApiManager shareMangager].tokenModel){
//                    [[BaiduApiManager shareMangager] requestPostAccessToken];
//                }
//                
//                [ChatHttpRequest testRequet];
            }
            
            
        }];
        [self.networkReachabilityManager startMonitoring];
        [self queryNetworkStatus];
    }
    return self;
}


- (void)networkStatusChange {
    switch (self.networkReachabilityStatus) {
        case AFNetworkReachabilityStatusUnknown: self.networkStatus = @"未知网络";break;
        case AFNetworkReachabilityStatusReachableViaWiFi: self.networkStatus = @"WIFI";break;
        case AFNetworkReachabilityStatusReachableViaWWAN: self.networkStatus = @"2G/3G/4G";break;
        case AFNetworkReachabilityStatusNotReachable: self.networkStatus = @"未知网络";break;
        default:
            break;
    }
}

/// AFNetworkReachabilityManager并不能在启动时第一时间获取网络状态,所以要主动查询.
- (void)queryNetworkStatus {
    struct sockaddr_in localWifiAddress;
    bzero(&localWifiAddress, sizeof(localWifiAddress));
    localWifiAddress.sin_len = sizeof(localWifiAddress);
    localWifiAddress.sin_family = AF_INET;
    localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
    SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)&localWifiAddress);
    SCNetworkReachabilityFlags flags;
    if (SCNetworkReachabilityGetFlags(ref, &flags)) {
        self.networkReachabilityStatus = [self networkReachabilityStatusForFlags:flags];
        [self networkStatusChange];
    }
}

- (AFNetworkReachabilityStatus)networkReachabilityStatusForFlags:(SCNetworkReachabilityFlags)flags {
    BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0);
    BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0);
    BOOL canConnectionAutomatically = (((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || ((flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0));
    BOOL canConnectWithoutUserInteraction = (canConnectionAutomatically && (flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0);
    BOOL isNetworkReachable = (isReachable && (!needsConnection || canConnectWithoutUserInteraction));

    AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusUnknown;
    if (isNetworkReachable == NO) {
        status = AFNetworkReachabilityStatusNotReachable;
    }
#if    TARGET_OS_IPHONE
    else if ((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0) {
        status = AFNetworkReachabilityStatusReachableViaWWAN;
    }
#endif
    else {
        status = AFNetworkReachabilityStatusReachableViaWiFi;
    }

    return status;
}


@end
